package com.Controller;

import com.Service.ImageService;
import com.alibaba.fastjson.JSONObject;
import com.until.MakeParameters;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.Map;

@Controller
@RequestMapping("Image")
public class HttpRequest {

    @Autowired
    private ImageService imageService;

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public static String  queryImage(String Action, String Region) throws Exception{
        Map<String,String > map = MakeParameters.parameters();
        String urlNameString = MakeParameters.makeSignature(map);
        return urlNameString;
    }

@RequestMapping(value = "")
    public static JSONObject sengGet4(String Action, String Region){
        try {
            String date = MakeParameters.formatIso8601Date();
            String url = queryImage(Action,Region);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建get对象，设置url
            HttpGet get = new HttpGet(url);
            //添加请求头信息
            get.setHeader("Content-Type","application/json;charset=UTF-8");
            get.setHeader("Accept-Encoding", "*");
            get.setHeader("Date",date);
            //执行get请求
            CloseableHttpResponse response = httpClient.execute(get);
            //获取服务器响应内容
            String str = EntityUtils.toString(response.getEntity());
            response.close();
            httpClient.close();
            System.out.println(str);
            JSONObject jsonObject = JSONObject.parseObject(str);
            return jsonObject;
        }catch (Exception e){
            System.out.println("异常");
            return null;
        }
    }

    public static void main(String[] args) throws Exception{
        JSONObject js =  sengGet4("","");
        System.out.println(js);
    }

}
